package com.jww.service.impl;

import com.github.pagehelper.PageHelper;
import com.jww.domain.store.Question;
import com.jww.mapper.store.QuestionMapper;
import com.jww.service.QuesTionService;
import com.jww.utils.MyUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class QuesTionServiceImpl implements QuesTionService {

    @Override
    public List<Question> selectAll(String pageNum, String pageSize) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        QuestionMapper mapper = sqlSession.getMapper(QuestionMapper.class);
        int num=1;
        int count=10;
        if (pageNum!=null){
            num=Integer.parseInt(pageNum);
        }
        if (pageSize!=null){
            count=Integer.parseInt(pageSize);
        }
        PageHelper.startPage(num,count);
        List<Question> list = mapper.selectAll();
        MyUtils.close(sqlSession);
        return list;
    }

    @Override
    public List<Question> list() {
        SqlSession sqlSession = MyUtils.getSqlSession();
        QuestionMapper mapper = sqlSession.getMapper(QuestionMapper.class);
        List<Question> list = mapper.selectAll();
        MyUtils.close(sqlSession);
        return list;
    }

    @Override
    public String save(Question question,boolean flag) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        QuestionMapper mapper = sqlSession.getMapper(QuestionMapper.class);
        //id使用UUID的生成策略来获取
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        question.setId(id);
        //设置新创建的题目默认的审核状态为未审核（0）
        question.setReviewStatus("0");
        //设置创建时机
        question.setCreateTime(new Date());
      //前端有上传文件操作，记录文件名，否则不记录
        if (flag){
            //设置当前存取的图片名称为id
            question.setPicture(id);
        }

        mapper.save(question);
        MyUtils.close(sqlSession);
        return id;
    }

    @Override
    public boolean delete(String ids) {
        SqlSession sqlSession =null;
        boolean flag=false;
        try {
            sqlSession = MyUtils.getSqlSession();
            QuestionMapper mapper = sqlSession.getMapper(QuestionMapper.class);
            String[] idss = ids.split(",");
            mapper.delete(idss);
            MyUtils.close(sqlSession);
            flag=true;
        }catch (Exception e){
            throw new RuntimeException("删除失败");
        }finally {
            if (sqlSession!=null){
                sqlSession.close();
            }
        }
        return flag;
    }

    @Override
    public Question selectById(String id) {

        SqlSession sqlSession = MyUtils.getSqlSession();
        QuestionMapper mapper = sqlSession.getMapper(QuestionMapper.class);

        Question question = mapper.selectById(id);
        MyUtils.close(sqlSession);
        return question;
    }

    public void update(Question question,boolean flag) {
        SqlSession sqlSession = MyUtils.getSqlSession();
        QuestionMapper mapper = sqlSession.getMapper(QuestionMapper.class);
        //前端有上传文件操作，记录文件名，否则不记录
        if (flag){
            //设置当前存取的图片名称为id
            question.setPicture(question.getId());
        }
        mapper.update(question);
        MyUtils.close(sqlSession);
    }

    @Override
    public ByteArrayOutputStream uploadReport() {
        ByteArrayOutputStream os=null;
        SqlSession sqlSession = MyUtils.getSqlSession();
        QuestionMapper mapper = sqlSession.getMapper(QuestionMapper.class);
        List<Question> list = mapper.selectAll();
        //1.获取到对应的Excel文件，工作簿文件
        XSSFWorkbook wb = new XSSFWorkbook();
        //创建工作表
        XSSFSheet s = wb.createSheet("题目数据文件");
        //设置通用配置
        XSSFCellStyle cs_field = wb.createCellStyle();
        //设置水平居中
        cs_field.setAlignment(HorizontalAlignment.CENTER);
        //设置水平线
        cs_field.setBorderTop(BorderStyle.THIN);
        cs_field.setBorderBottom(BorderStyle.THIN);
        cs_field.setBorderRight(BorderStyle.THIN);
        cs_field.setBorderLeft(BorderStyle.THIN);
        //制作标题
        // 合并单元格
       s.addMergedRegion(new CellRangeAddress(1,1,1,12));
       //创建行
        XSSFRow row_1 = s.createRow(1);
        //创建列
        XSSFCell cell_1_1 = row_1.createCell(1);
        //单元格赋值
        cell_1_1.setCellValue("在线试题导出信息");
        //创建表头样式
        CellStyle cs_title = wb.createCellStyle();
        //水平居中
        cs_title.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        cs_title.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置样式
        cell_1_1.setCellStyle(cs_title);

        //制作表头
        String[] fields = {"题目ID","所属公司ID","所属目录ID","题目简介","题干描述",
                "题干配图","题目分析","题目类型","题目难度","是否经典题","题目状态","审核状态"};
        XSSFRow row_2 = s.createRow(2);
        //循环添加表头数据
        for (int i=0;i<fields.length;i++){
            XSSFCell cell_2_temp = row_2.createCell(i + 1);
            cell_2_temp.setCellValue(fields[i]);
            cell_2_temp.setCellStyle(cs_field);
        }

        //制作数据
        int row_index=0;
        for (Question q:list){
            int cell_index=0;
            XSSFRow row_temp = s.createRow(3 + row_index++);

            XSSFCell cell_data_1 = row_temp.createCell(1 + cell_index++);
            cell_data_1.setCellValue(q.getId());
            cell_data_1.setCellStyle(cs_field);

            XSSFCell cell_data_2 = row_temp.createCell(1 + cell_index++);
            cell_data_2.setCellValue(q.getCompanyId());
            cell_data_2.setCellStyle(cs_field);

            XSSFCell cell_data_3 = row_temp.createCell(1 + cell_index++);
            cell_data_3.setCellValue(q.getCatalogId());
            cell_data_3.setCellStyle(cs_field);

            XSSFCell cell_data_4 = row_temp.createCell(1 + cell_index++);
            cell_data_4.setCellValue(q.getRemark());
            cell_data_4.setCellStyle(cs_field);

            XSSFCell cell_data_5 = row_temp.createCell(1 + cell_index++);
            cell_data_5.setCellValue(q.getSubject());
            cell_data_5.setCellStyle(cs_field);

            XSSFCell cell_data_6 = row_temp.createCell(1 + cell_index++);
            cell_data_6.setCellValue(q.getPicture());
            cell_data_6.setCellStyle(cs_field);

            XSSFCell cell_data_7 = row_temp.createCell(1 + cell_index++);
            cell_data_7.setCellValue(q.getAnalysis());
            cell_data_7.setCellStyle(cs_field);

            XSSFCell cell_data_8 = row_temp.createCell(1 + cell_index++);
            cell_data_8.setCellValue(q.getType());
            cell_data_8.setCellStyle(cs_field);

            XSSFCell cell_data_9 = row_temp.createCell(1 + cell_index++);
            cell_data_9.setCellValue(q.getDifficulty());
            cell_data_9.setCellStyle(cs_field);

            XSSFCell cell_data_10 = row_temp.createCell(1 + cell_index++);
            cell_data_10.setCellValue(q.getIsClassic());
            cell_data_10.setCellStyle(cs_field);

            XSSFCell cell_data_11 = row_temp.createCell(1 + cell_index++);
            cell_data_11.setCellValue(q.getState());
            cell_data_11.setCellStyle(cs_field);

            XSSFCell cell_data_12 = row_temp.createCell(1 + cell_index++);
            cell_data_12.setCellValue(q.getReviewStatus());
            cell_data_12.setCellStyle(cs_field);

        }

        try {
            //将内层中数据写入到流中
            os = new ByteArrayOutputStream();
            wb.write(os);
            wb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return os;
    }
}
